package org.ainlolcat.ainscience2.controller.impl;

import org.ainlolcat.ainscience2.controller.Controller;
import org.ainlolcat.ainscience2.controller.loader.ConfigurationLoader;
import org.ainlolcat.ainscience2.controller.loader.impl.ConfigurationLoaderImpl;
import org.ainlolcat.ainscience2.model.Configuration;
import org.ainlolcat.ainscience2.model.Entity;
import org.ainlolcat.ainscience2.model.Project;
import org.ainlolcat.ainscience2.model.filter.Filter;
import org.ainlolcat.ainscience2.model.filter.FilterApplier;
import org.ainlolcat.ainscience2.model.impl.ProjectImpl;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Collection;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: ain
 * Date: 17.06.12
 * Time: 0:03
 * To change this template use File | Settings | File Templates.
 */
public class ControllerImpl implements Controller {
    static Logger log = Logger.getLogger(ControllerImpl.class.getName());
    Project project;

    public Project getProject() {
        return project;
    }

    public void loadProject(String filename) {
        try {
            loadProject(new FileReader(filename));
        } catch (FileNotFoundException e) {
            log.log(Level.WARNING, "Error while load project", e);
            throw new RuntimeException(e);
        }
    }

    public void loadProject(Reader reader) {
        try {
            Document doc = new SAXBuilder().build(reader);
            Configuration configuration = new ConfigurationLoaderImpl().loadConfiguration(doc, true);
            project = new ProjectImpl();
            project.setConfiguration(configuration);
        } catch (Exception e) {
            log.log(Level.WARNING, "Error while load Configuration", e);
            throw new RuntimeException(e);
        }
    }
}
